/**    
 * 文件名：QRCODETool.java    
 *    
 * 版本信息：    
 * 日期：2015-7-29    
 * Copyright inspursoftware Corporation 2015     
 * 版权所有    
 *    
 */
package com.thinkgem.jeesite.common.utils.wx;

import java.io.File;
import java.util.Hashtable;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;

/**    
 *     
 * 项目名称：ALM_eb_web    
 * 类名称：QRCODETool    
 * 类描述：    
 * 创建人：zhaoshuhe    
 * 创建时间：2015-7-29 下午5:05:14    
 * @version     
 *     
 */
public class QRCODETool {
    private static Log log = LogFactory.getLog(QRCODETool.class);
    public static String getQrCodePic(HttpServletRequest request,String url){
        int width = 300; 
        int height = 300; 
        //二维码的图片格式 
        String format = "gif"; 
        String dir = request.getSession().getServletContext().getRealPath("upload");
        File file = new File(dir);
        if (!file.exists()) {
            file.mkdirs();
        }
        String picName = UUIDTool.getRandomWithTime(4);
        try{
            Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); 
            //内容所使用编码 
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); 
            BitMatrix bitMatrix = new MultiFormatWriter().encode(url, 
                    BarcodeFormat.QR_CODE, width, height, hints); 
            //生成二维码  
            File outputFile = new File(dir +File.separator+ picName+".gif");
            MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);
        }catch(RuntimeException e){
        	e.printStackTrace();
            log.error("运行时异常");
        }catch (Exception e) {
           log.error(e.getMessage());
        }
        return picName + "." + format;
    }
}
